package com.hb.repository.impl;

import com.hb.model.Order;
import com.hb.repository.OrderOperations;
import com.sun.xml.internal.ws.server.sei.EndpointResponseMessageBuilder;
import org.neo4j.helpers.collection.IteratorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.conversion.Result;
import org.springframework.data.neo4j.template.Neo4jOperations;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 实现中间接口
 */
public class OrderRepositoryImpl implements OrderOperations {
    // 注入的具体来讲就是 Neo4jTemplate 的实例
    private final Neo4jOperations neo4j;

    @Autowired
    public OrderRepositoryImpl(Neo4jOperations neo4j) {
        this.neo4j = neo4j;
    }

    @Override
    public List<Order> findSiAOrders() {
        Map<String, Object> m = new HashMap<>();
        Result<Map<String, Object>> result = neo4j.query("match (o:Order)-[:HAS_ITEMS]->(i:Item) where i.product='Spring in Action' return o", m);
        EndResult<Order> endResult = result.to(Order.class);
        return IteratorUtil.asList(endResult);
    }
}
